package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.AjaxResult;
import com.sofwin.system.pojo.SysRole;
import com.sofwin.system.service.ResourceService;
import com.sofwin.system.service.RoleService;
import com.sofwin.system.vo.ResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/7/14 16:27
 * @email 115176513@qq.com
 * @description: TODO
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @GetMapping("data")
    public PageInfo<SysRole> data(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = "10") Integer pageSize){
        return roleService.queryRoleByPage(role,pageNumber,pageSize);
    }

    @RequestMapping("save")
    public AjaxResult save(SysRole role){
        boolean status = roleService.saveOrUpdateRole(role);
        if(status){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
    @RequestMapping("delete")
    public AjaxResult delete(Integer roleId){
        boolean status = roleService.removeById(roleId);
        if(status){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
    @RequestMapping("deletes")
    public AjaxResult deletes(@RequestParam(name="ids[]") Integer[] ids){
        boolean status = roleService.removleByIds(ids);
        if(status){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @PostMapping("permission")
    public AjaxResult permission(Integer roleId){
        List<ResourceVO> resources = resourceService.queryAllResouces(roleId);
        return AjaxResult.success(resources);
    }
    @RequestMapping("savePermission")
    public AjaxResult savePermission(@RequestParam(name = "ids[]") Integer[] ids,Integer roleId){
        boolean status = roleService.saveRolePermission(roleId,ids);
        if(status) return AjaxResult.success();
        return AjaxResult.error();
    }
}
